import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useUserStore } from '../store/loginStore';

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    meta: { title: '首页' },
    component: HomeView
  },
  {
    path: '/list',
    name: 'list',
    meta: { title: '列表' },

    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/List.vue')
  },
  {
    path: '/detail/:id',
    name: 'detail',
    meta: { title: '详情' },

    component: () => import(/* webpackChunkName: "about" */ '../views/Detail.vue')
  },
  {
    path: '/user',
    name: 'user',
    meta: { requiredLogin: true,title: '个人中心' },
    component: () => import(/* webpackChunkName: "about" */ '../views/user/Index.vue'),
    children: [
      {
        path: '/user/order',
        name: 'order',
        meta: { title: '我的订单' ,requiredLogin: true},

        component: () => import('@/views/user/Order.vue'),
      },
      {
        path: '/user/collectionRecord',
        name: 'collectionRecord',
        meta: { title: '收藏记录',requiredLogin: true },

        component: () => import('@/views/user/CollectionRecord.vue'),
      },
      {
        path: '/user/issuingReceipt',
        name: 'IssuingReceipt',
        meta: { title: '开票' ,requiredLogin: true},
        component: () => import('@/views/user/IssuingReceipt.vue'),
      },
      // browsingHistory
      {
        path: '/user/browsingHistory',
        name: 'browsingHistory',
        meta: { title: '浏览记录' ,requiredLogin: true},
        component: () => import('@/views/user/BrowsingHistory.vue'),
      },
      // /user/InvoiceList
      {
        path: '/user/ticketHeader',
        name: 'ticketHeader',
        meta: { title: '开票头管理' ,requiredLogin: true},
        component: () => import('@/views/user/TicketHeader.vue'),
      },
      // /user/machineCode
      {
        path: '/user/machineCode',
        name: 'machineCode',
        meta: { title: '机器码' ,requiredLogin: true},
        component: () => import('@/views/user/MachineCode.vue'),
      },
    ]
  },
  {
    path: '/helpcenter',
    name: 'helpcenter',
    meta: { title: '帮助中心'},

    component: () => import(/* webpackChunkName: "about" */ '../views/help/Index.vue')
  },
  {
    path: '/buy',
    name: 'buy',
    meta: { title: '付款' ,requiredLogin: true},
    component: () => import('@/views/Buy.vue'),
  },
  {
    path: '/PayInform',
    name: 'PayInform',
    meta: { title: '付款状态' ,requiredLogin: true},
    component: () => import('@/views/PayInform.vue'),
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/',
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// 全局前置守卫
router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.title) {
    document.title = to.meta.title +''// 设置页签标题
  }
  next()
})
router.beforeEach((to, from, next) => {
  window.scrollTo(0, 0);
  const userStore = useUserStore()
  userStore.getUserAuth()
  const { requiredLogin } = to.meta
  if (!userStore.isLogin && requiredLogin) {
    next('/')
  } else {
    next()
  }
})
export default router
